//
//  CustomArcView.m
//  cheshi
//
//  Created by CKF on 2025/5/29.
//  Copyright © 2025 Mac. All rights reserved.
//

#import "CustomArcView.h"
#import "TimeAlertView.h"
@interface CustomArcView ()
@property (nonatomic, assign) CGFloat touchAngle1;
//@property (nonatomic, assign) CGFloat touchAngle2;
@property (nonatomic, assign) CGFloat startAngle;
@property (nonatomic, assign) CGFloat endAngle;
@property (nonatomic, assign) CGFloat lineWidth;
@property (nonatomic, assign) CGFloat innerMinAngle;
@property (nonatomic, strong) TimeAlertView *timeView;
@end

@implementation CustomArcView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.startAngle = 3/4.0 * M_PI;
        self.endAngle = 1/4.0 * M_PI;
        self.touchAngle1 = self.startAngle;
//        self.touchAngle2 = self.startAngle;
        self.innerMinAngle = self.startAngle-(1/360.0* M_PI);
        self.lineWidth = 14;
        
        _imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth-100, KScreenWidth-100)];
        _imgView.image = [UIImage imageNamed:@"lamp_fan"];
        _imgView.center = self.center;
        [self addSubview:_imgView];
        
        UIButton * timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.frame = CGRectMake(30, frame.size.height-70, 44, 44);
        [timeBtn setImage:[UIImage imageNamed:@"Timer"] forState:0];
        [timeBtn addTarget:self action:@selector(timeBtnAct) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:timeBtn];
        
        _speedLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, CGRectGetMidY(timeBtn.frame)-13, KScreenWidth-160, 26)];
        [self addSubview:_speedLabel];
        _speedLabel.font = [UIFont systemFontOfSize:20];
        _speedLabel.textColor = RGB(17, 17, 17);
        _speedLabel.text = [NSString stringWithFormat:@"%@%@%@%@",LocalString(@"风扇速度"),LocalString(@"："),@"0",LocalString(@"档")];
        _speedLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

-(void)timeBtnAct{
    if (!_timeView) {
        _timeView = [[TimeAlertView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _timeView.buttonBlock = ^(NSInteger i) {
            
        };
        [[UIApplication sharedApplication].delegate.window addSubview:_timeView];
    }else{
        _timeView.hidden = NO;
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGFloat radius1 = MIN(CGRectGetWidth(rect), CGRectGetHeight(rect)) / 2 - 20;
//    CGFloat radius2 = MIN(CGRectGetWidth(rect), CGRectGetHeight(rect)) / 2 - 50;
    
    // 绘制灰色弧线
    UIBezierPath *grayPath1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius1 startAngle:self.startAngle endAngle:self.endAngle clockwise:YES];
    grayPath1.lineCapStyle = kCGLineCapRound;
    grayPath1.lineWidth = self.lineWidth;
    [RGB(142, 149, 146) setStroke];
    [grayPath1 stroke];
    
//    // 绘制灰色弧线
//    UIBezierPath *grayPath2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius2 startAngle:self.startAngle endAngle:self.endAngle clockwise:YES];
//    grayPath2.lineCapStyle = kCGLineCapRound;
//    grayPath2.lineWidth = self.lineWidth;
//    [RGB(142, 144, 148) setStroke];
//    [grayPath2 stroke];
    
    // 绘制红色弧线
    UIBezierPath *redPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius1 startAngle:self.innerMinAngle endAngle:self.touchAngle1 clockwise:YES];
    redPath.lineWidth = self.lineWidth;
    redPath.lineCapStyle = kCGLineCapRound;
    [RGB(38, 156, 241) setStroke];
    [redPath stroke];
    
    
//    // 绘制红色弧线
//    UIBezierPath *orangePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius2 startAngle:self.innerMinAngle endAngle:self.touchAngle2 clockwise:YES];
//    orangePath.lineWidth = self.lineWidth;
//    orangePath.lineCapStyle = kCGLineCapRound;
//    [RGB(255, 135, 50) setStroke];
//    [orangePath stroke];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self handleTouch:touches];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self handleTouch:touches];
}

- (void)handleTouch:(NSSet<UITouch *> *)touches {
    UITouch *touch = [touches anyObject];
    if (!touch) return;
    
    CGPoint touchPoint = [touch locationInView:self];
    

    // 判断触摸点是否在弧线上
    if ([self isPointOnArcBlue:touchPoint]) {
        CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
        
        // 计算触摸点的角度
        CGFloat dx = touchPoint.x - center.x;
        CGFloat dy = touchPoint.y - center.y;
        CGFloat angle = atan2(dy, dx);
        
        // 确保角度在弧线范围内
        if (angle >= self.startAngle || angle <= self.endAngle+10/180.0*M_PI) {
            if (angle>self.endAngle && angle <= self.endAngle+10/180.0*M_PI) {
                self.touchAngle1 = self.endAngle;
            }else{
                self.touchAngle1 = angle;
            }
            [self setNeedsDisplay];
        }
        float realAngle = angle * 180 / M_PI;
        if (_callBack) {
            if (realAngle>=134 && realAngle<=180) {
                _callBack(0);
            }else if (realAngle<=-135) {
                _callBack(1);
            }else if (realAngle<=-90) {
                _callBack(2);
            }else if (realAngle<=-45) {
                _callBack(3);
            }else if (realAngle<=0) {
                _callBack(4);
            }else if (realAngle<44) {
                _callBack(5);
            }else  {
                _callBack(6);
            }
        }
        NSLog(@"angle%f",angle * 180 / M_PI);
//  
    }else{
        NSLog(@"不在");
    }
}


- (BOOL)isPointOnArcBlue:(CGPoint)point {
    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) / 2 - 20;
    // 计算触摸点到圆心的距离
    CGFloat dx = point.x - center.x;
    CGFloat dy = point.y - center.y;
    CGFloat distance = sqrt(dx * dx + dy * dy);
    
    float number = fabs(distance - radius);
    NSLog(@"number------%f",number);
    // 检查距离是否在弧线宽度范围内
    BOOL isInDistanceRange = number <= self.lineWidth / 2;
    // 计算触摸点的角度
    CGFloat angle = atan2(dy, dx);
    
    // 确保角度在弧线范围内
    BOOL isInAngleRange = angle >= self.startAngle || angle <= (self.endAngle+(10/180.0*M_PI));
    
    return isInDistanceRange && isInAngleRange;
}

//- (BOOL)isPointOnArcOrange:(CGPoint)point {
//    CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
//    CGFloat radius = MIN(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) / 2 - 50;
//    // 计算触摸点到圆心的距离
//    CGFloat dx = point.x - center.x;
//    CGFloat dy = point.y - center.y;
//    CGFloat distance = sqrt(dx * dx + dy * dy);
//    
//    float number = fabs(distance - radius);
//    NSLog(@"number------%f",number);
//    // 检查距离是否在弧线宽度范围内
//    BOOL isInDistanceRange = number <= self.lineWidth / 2;
//    // 计算触摸点的角度
//    CGFloat angle = atan2(dy, dx);
//    
//    // 确保角度在弧线范围内
//    BOOL isInAngleRange = angle >= self.startAngle || angle <= self.endAngle+10/180.0*M_PI;
//    
//    return isInDistanceRange && isInAngleRange;
//}

@end
